package initialize

import (
	"github.com/cy77cc/hioshop/global"
	"github.com/cy77cc/hioshop/middleware"
	"github.com/cy77cc/hioshop/routers"
	"github.com/gin-gonic/gin"
)

func InitApp() *gin.Engine {

	global.REDIS, _ = InitRedisDB(global.CONFIG.Redis)
	global.DB = InitGorm(global.CONFIG.Mysql)

	//zap.ReplaceGlobals(global.LOG)
	gin.SetMode(global.CONFIG.System.Mode)

	Migrate()

	engine := gin.Default()

	apiShop := &routers.ApiShop{}
	apiShop.InitShopApi("/api", engine)

	apiAdmin := &routers.ApiAdmin{}
	apiAdmin.InitAdminApi("/admin", engine)

	//engine.Static("/static", "../../static")
	//engine.LoadHTMLGlob("static/**/*")
	engine.Use(middleware.Cors(), middleware.CacheMiddleware())
	engine.GET("/static/*filepath", func(c *gin.Context) {
		filepath := c.Param("filepath")
		c.File("/app/static/" + filepath)
	})

	engine.Use(gin.Recovery())
	go GenToken()

	return engine
}
